page.tsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use client";
  2. import { getShopInfoApi, getShopListApi, ShopInfo } from "@/api/depositsApi";
  3. import Loading from "@/components/Loading";
  4. import React from "react";
  5. import DepositData from "./DepositData";
  6. import Left from "./Left";
  7. const Deposit = () => {
  8. const [actIdx, setActIdx] = React.useState(0);
  9. const [shopTypeList, setShopTypeList] = React.useState<any[]>([]);
  10. const [currentChannel, setCurrentChannel] = React.useState<any>({});
  11. const [shopInfo, setShopInfo] = React.useState<ShopInfo>({} as ShopInfo);
  12. const [loading, setLoading] = React.useState(false);
  13. const actChange = (idx: number) => {
  14. setActIdx(idx);
  15. doChangeChannel(shopTypeList[idx].pay_channel[0]); //TODO: dele
  16. };
  17. const currentShop = React.useMemo(() => {
  18. return shopTypeList[actIdx];
  19. }, [actIdx, shopTypeList]);
  20. React.useEffect(() => {
  21. getData();
  22. }, []);
  23. React.useEffect(() => {
  24. const id = shopTypeList[actIdx]?.id;
  25. getInfo(id);
  26. }, [actIdx, shopTypeList]);
  27. const getData = async () => {
  28. const res = await getShopListApi();
  29. if (res?.code === 200 && res?.data) {
  30. setShopTypeList(res?.data || []);
  31. if (res?.data[actIdx]?.pay_channel[0]) {
  32. doChangeChannel(res?.data[actIdx]?.pay_channel[0]);
  33. }
  34. }
  35. };
  36. const getInfo = async (id: number) => {
  37. if (!id) {
  38. setShopInfo({} as ShopInfo);
  39. return;
  40. }
  41. try {
  42. setLoading(true);
  43. const res = await getShopInfoApi({ shop_id: id });
  44. if (res?.code === 200 && res?.data?.products?.length) {
  45. res.data.products.sort((a: any, b: any) => {
  46. return a.pay - b.pay;
  47. }); //TODO: sort sho
  48. setShopInfo(res.data);
  49. }
  50. } finally {
  51. setLoading(false);
  52. }
  53. };
  54. const doChangeChannel = (data: any) => {
  55. setCurrentChannel(data);
  56. };
  57. console.log(9991, currentChannel);
  58. return (
  59. <div className="flex h-[100%] flex-row items-stretch">
  60. <div className="w-[1rem]">
  61. <Left
  62. data={shopTypeList}
  63. onChange={actChange}
  64. channelData={currentChannel}
  65. actIdx={actIdx}
  66. ></Left>
  67. </div>
  68. <div className="flex-1 overflow-auto">
  69. {!loading && (
  70. <DepositData
  71. onActiveChange={doChangeChannel}
  72. currentChannel={currentChannel}
  73. setChannel={doChangeChannel}
  74. shopInfo={shopInfo}
  75. channel={currentShop?.pay_channel || []}
  76. currentShop={currentShop}
  77. />
  78. )}
  79. {loading && (
  80. <div className="flex h-[100%] items-center justify-center">
  81. <Loading></Loading>
  82. </div>
  83. )}
  84. </div>
  85. </div>
  86. );
  87. };
  88. export default Deposit;